package com.freestyle.nettychat.chat.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.module.SimpleModule;

import com.freestyle.netty.easynetty.common.Utils;
import com.freestyle.nettychat.chat.CodeConsts;
import com.freestyle.nettychat.chat.deserializer.LogMessageDeserializer;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by rocklee on 2022/2/6 11:43
 */
@JsonDeserialize(using = LogMessageDeserializer.class)
public class LogMessage extends ChatMessage<List<ChatMessage>> {
  static{
    SimpleModule module=new SimpleModule();
    module.addDeserializer(LogMessage.class,new LogMessageDeserializer());
    Utils.getMapper(LogMessage.class).registerModule(module);
  }
  public static LogMessage fromMessage(long id, ChatMessage msg){
    msg.getProperties().setExtParams("time",new Date().getTime());
    LogMessage message=new LogMessage();
    message.setData(Arrays.asList(msg));
    ChatMessageProperties messageProperties=new ChatMessageProperties();
    messageProperties.setId(id);
    message.setProperties(messageProperties);
    messageProperties.setSClass(CodeConsts.MessageType.log.toString());
    return message;
  }
  public static LogMessage fromLog(long id,LinkedHashMap<ChatMessage,Long>logs){
    logs.entrySet().stream().forEach(e->{
      e.getKey().getProperties().setExtParams("time",e.getValue());
    });
    LogMessage message=new LogMessage();
    message.setData(logs.keySet().stream().collect(Collectors.toList()));
    ChatMessageProperties messageProperties=new ChatMessageProperties();
    messageProperties.setId(id);
    message.setProperties(messageProperties);
    messageProperties.setSClass(CodeConsts.MessageType.log.toString());
    return message;
  }

}
